﻿
function css_tr_mouse(array) {
    for (var j = 0; j < array.length; j++) {
        if (!document.getElementById(array[j])) return;
        var Ptable = document.getElementById(array[j]);
        var Ptr = document.getElementById(array[j]).getElementsByTagName("tr");
        var tmpClassName;

        if (document.getElementById("_hi" + array[j])) {
            document.getElementById("_hi" + array[j]).value = Ptable.outerHTML;
        }
        
        var intTemp = 1;
        for (var i = 0; i < Ptr.length; i++) {
            if (Ptr[i].header != "0") continue;
            if (Ptr[i].typesum != "0") continue;
            //设置双行颜色
            if (intTemp % 2 == 0) {
                Ptr[i].className = "GridTr_DataDouble";
            }

//            Ptr[i].onmouseover = function() {
//                if (!isLoadCss) return;
//                tmpClassName = this.className;
//                this.className = "GridTr_DataMove";
//            };
//            Ptr[i].onmouseout = function() {
//                if (!isLoadCss) return;
//                this.className = tmpClassName;
//            };

            intTemp++;

        }
    }
}

//单击时设置行的颜色
function click_tr_color(objtr) {
    if (objtr.typesum != "0") return;
}

//由于点了排序事件之后，鼠标移动行的颜色事件会丢失，所以需要重新加载
function css_tr_mouse_for_order(tableobjId) {
    if (!document.getElementById(tableobjId)) return;
    var Ptr = document.getElementById(tableobjId).getElementsByTagName("tr");
    var tmpClassName;
    for (var i = 1; i < Ptr.length; i++) {
        if (Ptr[i].typesum != "0") continue;

        //设置双行颜色
        if (i % 2 == 0) {
            Ptr[i].className = "GridTr_DataDouble";
        }

        Ptr[i].onmouseover = function() {
            tmpClassName = this.className;
            this.className = "GridTr_DataMove";
        };
        Ptr[i].onmouseout = function() {
            this.className = tmpClassName;
        };

    }
}

//Table 排序	
function ObjTableSort(tableobjId, colNum) {
    //定义相关对象
    var tableobj, headEventObject, trObject, Object;
    if (!document.getElementById(tableobjId)) return;
    //获取Table对象
    tableobj = document.getElementById(tableobjId);
    //取得引发事件的对象 
    headEventObject = event.srcElement;
    while (headEventObject.tagName != "TR") {
        //不是tr行,则从底下的td冒泡上来寻找到相应行
        headEventObject = headEventObject.parentElement;
    }

    var tableRows = 0;
    //取得表格中所有行对象
    trObject = tableobj.children[0].children;
    for (i = 0; i < trObject.length; i++) {
        //取得每行的对象
        Object = tableobj.children[0].children[i];
        //如果不是忽略行,则行数加一(标题行不参加计算)  
        tableRows = (trObject[i].order == '0') ? tableRows : tableRows + 1;
    }

    var trinnerHTML = new Array(tableRows);
    var tdinnerHTML = new Array(tableRows);
    var tdNumber = new Array(tableRows);
    var i0 = 0;
    var i1 = 0;
    var sourceHTML = "";
    var sSumHtml = ""; //总计
    for (i = 0; i < trObject.length; i++) {
        if (trObject[i].order == '1') {
            //把行放在数组里
            //trinnerHTML[i0]=trObject[i].innerHTML;
            //把要排序的行中td的内容放数组里   
            tdinnerHTML[i0] = trObject[i].children[colNum / 2].innerHTML;
            //行号
            tdNumber[i0] = i;
            //加一,下个循环用
            i0++;
        }
        //记录总计
        if (trObject[i].typesum == '2') {
            sSumHtml = trObject[i].outerHTML;
        }
    }
    //取得表格中所有tr的html代码
    sourceHTML = tableobj.children[0].outerHTML;

    //对所有td中的字符串进行排序,算不算冒泡排序   
    for (bi = 0; bi < tableRows; bi++) {
        for (i = 0; i < tableRows; i++) {
            if (tdinnerHTML[i] > tdinnerHTML[i + 1]) {
                t_s = tdNumber[i + 1];
                t_b = tdNumber[i];
                tdNumber[i + 1] = t_b;
                tdNumber[i] = t_s;
                temp_small = tdinnerHTML[i + 1];
                temp_big = tdinnerHTML[i];
                tdinnerHTML[i + 1] = temp_big;
                tdinnerHTML[i] = temp_small;
            }
        }
    }

    var showshow = '';
    var numshow = '';
    for (i = 0; i < tableRows; i++) {
        //把排序好的td的内容存在showshow字串里
        showshow = showshow + tdinnerHTML[i] + ' ';
        //把排序好的相应的行号也存在numshow中    
        numshow = numshow + tdNumber[i] + '|';
    }
    //从<TBODY>截断 
    sourceHTML_head = sourceHTML.split("<TBODY>");

    numshow = numshow.split("|");
    var trRebuildHTML = '';
    if (event.srcElement.title == event.srcElement.innerText) {
        //已点击的列,   则逆排   
        for (i = 0; i < tableRows; i++) {
            trRebuildHTML = trRebuildHTML + trObject[numshow[tableRows - 1 - i]].outerHTML; //取出排序好的tr的内容连接起来   
        }
        event.srcElement.title = '';
    }
    else {
        //默认顺排,新点击顺排   
        for (i = 0; i < tableRows; i++) {
            trRebuildHTML = trRebuildHTML + trObject[numshow[i]].outerHTML;
        }
        event.srcElement.title = event.srcElement.innerText;
    }
    //取得排序后的tr集合结果字符串   
    var DataRebuildTable = '';
    var sTemp;
    sTemp = tableobj.outerHTML.substring(0, tableobj.outerHTML.toLowerCase().indexOf("<tbody>"));
    //把旧的表格头和新的tr排序好的元素连接起来,   (修改了一下)   
    DataRebuildTable = sTemp + trObject[0].outerHTML + trRebuildHTML + sSumHtml + "</table>";
    tableobj.outerHTML = DataRebuildTable; //表格用新串重新写一次   

    css_tr_mouse_for_order(tableobjId);
}

//刷新数据表格
function RefleshDataTable(objtableid) {
    document.getElementById(objtableid).outerHTML = document.getElementById("_hi" + objtableid).value;
    css_tr_mouse_for_order(objtableid);
}